﻿using System;
using System.Windows.Forms;
using BLO;
using DTO;

namespace QuanLyBanHang
{
    public partial class frmChatLieu : Form
    {
        private readonly ChatLieu_Blo _chatLieuBlo = new ChatLieu_Blo();

        public frmChatLieu()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtMaChatLieu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void txtTenChatLieu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            LoadData();
            btnLuu.Enabled = false;
            txtTenChatLieu.Enabled = false;
            btnSua.Enabled = true;
            btnBoQua.Enabled = false;
            btnXoa.Enabled = true;
            txtMaChatLieu.Enabled = false;
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            txtMaChatLieu.Text = "";
            txtTenChatLieu.Enabled = true;
            txtTenChatLieu.Text = "";
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            btnBoQua.Enabled = true;
            btnXoa.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string msg = "";
            int result = 0;

            string tenChatLieu = txtTenChatLieu.Text.Trim();
            if (string.IsNullOrEmpty(txtMaChatLieu.Text.Trim()))
            {
                result = _chatLieuBlo.AddObject(new ChatLieu_Dto
                                                    {
                                                        MaChatLieu =
                                                            "CL" +
                                                            _chatLieuBlo.AutoIncrementIdObject(),
                                                        TenChatLieu = tenChatLieu
                                                    });
                msg = result > 0 ? "Thêm chất liệu thành công!" : "Thêm chất liệu không thành công!";
            }
            else
            {
                result = _chatLieuBlo.EditObject(txtMaChatLieu.Text.Trim(),
                                                 new ChatLieu_Dto { TenChatLieu = tenChatLieu });
                msg = result > 0 ? "Cập nhập chất liệu thành công!" : "Cập nhập chất liệu không thành công!";
                btnLuu.Enabled = false;
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnBoQua .Enabled= false;
                btnXoa.Enabled = true;
                txtTenChatLieu.Enabled = false;
            }
            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK,
                            result > 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);

            if (result > 0)
                LoadData();


        }

        private void frmChatLieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var chatlieuBlo = new ChatLieu_Blo();
            dgvChatLieu.DataSource = chatlieuBlo.GetAllObject();
            dgvChatLieu.Columns[0].HeaderText = "Mã chất liệu";
            dgvChatLieu.Columns[1].HeaderText = "Tên chất liệu";
        }

        private void dgvChatLieu_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvChatLieu.Rows[e.RowIndex].Cells[0].Value == null || txtMaChatLieu.Enabled == true) return;

            txtMaChatLieu.Text = dgvChatLieu.Rows[e.RowIndex].Cells["MaChatLieu"].Value.ToString();
            txtTenChatLieu.Text = dgvChatLieu.Rows[e.RowIndex].Cells["TenChatLieu"].Value.ToString();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMaChatLieu.Text)) return;

            txtTenChatLieu.Enabled = true;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            btnBoQua.Enabled = true;
            btnXoa.Enabled = false;
        }

        private void txtTenChatLieu_TextChanged(object sender, EventArgs e)
        {
            if (txtTenChatLieu.Enabled.Equals(true))
                btnLuu.Enabled = !string.IsNullOrEmpty(txtTenChatLieu.Text);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa chất liệu này?", "Thông báo", MessageBoxButtons.YesNo,
                                              MessageBoxIcon.Warning);

            if (dr.Equals(DialogResult.No)) return;

            int result = _chatLieuBlo.DeleteObject(txtMaChatLieu.Text.Trim());
            string msg = result > 0 ? "Xóa dữ liệu thành công!" : "Xóa dữ liệu không thành công!";
            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK,
                            result > 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            LoadData();
        }
    }
}